package com.falcon.service.impl;

import java.util.List;

import com.falcon.domain.dos.ExamDO;
import com.falcon.domain.po.ExamPO;
import com.falcon.domain.req.ExamQuery;
import com.falcon.domain.req.ExamRequest;
import com.falcon.mapper.IExamMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.bean.BeanUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swan.mybatis.condition.Condition;
import com.falcon.mvc.domain.BasePager;
import com.falcon.core.utils.BeanUtils;

import com.falcon.service.IExamService;


@Slf4j
@Service
public class ExamService implements IExamService {

    @Autowired
    private IExamMapper examMapper;

    @Override
    public Long save(ExamRequest request){
        ExamPO examPO = BeanUtil.copyProperties(request, ExamPO.class);

        this.examMapper.insertNotNull(examPO);

        return examPO.getId();
    }

    @Override
    public Boolean update(Long id, ExamRequest request){

        ExamPO examPO = BeanUtil.copyProperties(request, ExamPO.class);
        examPO.setId(id);

        return this.examMapper.updateNotNull(examPO);
    }

    @Override
    public Boolean deleteById(Long id) {

        return this.examMapper.deleteById(id);
    }

    @Override
    public ExamDO queryById(Long id){

        ExamPO examPO = this.examMapper.selectById(id);

        ExamDO examDO = BeanUtil.copyProperties(examPO, ExamDO.class);

        return examDO;
    }


    @Override
    public List<ExamDO> queryList(ExamQuery request){

        Condition condition = request.toCondition();

        List<ExamPO> poList = this.examMapper.selectList(condition);

        List<ExamDO> doList = BeanUtil.copyToList(poList, ExamDO.class);

        return doList;
    }

    @Override
    public BasePager<ExamDO> queryPager(ExamQuery request){


        PageInfo<ExamPO> pageInfo = PageHelper.startPage(request.getPage(), request.getPageSize())
                .doSelectPageInfo(() -> this.examMapper.selectList(request.toCondition()));

        return BeanUtils.toPager(pageInfo, ExamDO.class);
    }

    @Override
    public int batchDelete(List<Long> ids) {

        return this.examMapper.deleteByIds(ids);
    }


}

